package com.love_weinuo.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.bean.EarCallbackBean;
import com.love_weinuo.databinding.ActivityMyCommisionBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.adapter.MyCommissionAdapter;
import com.love_weinuo.ui.adapter.OnItemClickListener;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by SensYang on 2019/03/01 11:07
 */

public class MyCommissionActivity extends BaseActivity {

    MyCommissionAdapter adapter;
    List<EarCallbackBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    List<EarCallbackBean.ResultEntity.ResEntity> resList = new ArrayList<>();
    ActivityMyCommisionBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_commision);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        setBack();
        setTitleText("我的佣金");
        getMyEar();
        adapter = new MyCommissionAdapter(this,strs);
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    String userID = (String) SpUtils.get("module_user_id","");
    public void getMyEar(){
        HTTPAPI.getInstance().getMyEar(userID, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("收益获取成功"+ s);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(s);
                    if (0 != Integer.valueOf((Integer) jsonObject.get("errcode"))) {
                        showInCommonDialog(jsonObject.get("msg").toString());
                        return;
                    }else{
                        EarCallbackBean bean = GsonUtil.GsonToBean(s,EarCallbackBean.class);
                        resList = bean.getResult().getRes();
                        strs.clear();
                        strs.addAll(resList);
                        adapter.notifyDataSetChanged();
                        if (strs.size() != 0) {
                            binding.recycler.setVisibility(View.VISIBLE);
                            binding.ilModuleNothing.setVisibility(View.GONE);
                        }else{
                            binding.recycler.setVisibility(View.GONE);
                            binding.ilModuleNothing.setVisibility(View.VISIBLE);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("收益获取失败"+ e.getMessage());
            }
        });
    }
}
